/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yeedal.common;

/**
 * File name extension
 *
 * @author goalboy
 */
public enum FileExtension {

    EMPTY(null, null),
    TXT("Plain Text", ".txt"),
    XLF("XLIFF", ".xlf"),
    XLIFF("XLIFF", ".xliff"),
    SDLXLF("SDL XLIFF", ".sdlxlf"),
    XLSX("Microsoft Excel 2007/2010", ".xlsx"),
    XLTX("Microsoft Excel 2007/2010", ".xltx"),
    PPTX("Microsoft PowerPoint 2007/2010", ".pptx"),
    POTX("Microsoft PowerPoint 2007/2010", ".potx"),
    DOCX("Microsoft Word 2007/2010", ".docx"),
    DOTX("Microsoft Word 2007/2010", ".dotx"),
    LOCK("Project Lock", ".lock"),
    LCPROJECT("Configuration", ".lcp"),
    DAT("SAVE", ".dat"),
    TMX("TMX", ".tmx"),
    TBX("TBX", ".tbx"),
    XML("XML", ".xml"),
    CSV("CSV", ".csv"),
    TFPROP("TransFile Property", ".tfprop"),
    PDF("PDF", ".pdf");
    String m_name;
    String m_id;

    private FileExtension(String name, String id) {
        m_name = name;
        m_id = id;
    }

    public String getName() {
        return m_name;
    }

    public String getId() {
        return m_id;
    }

    public static FileExtension parse(String id) {
        if (null == id) {
            return EMPTY;
        }
        for (FileExtension ext : FileExtension.values()) {
            if (ext.getId() != null
                    && 0 == ext.getId().compareToIgnoreCase(id)) {
                return ext;
            }
        }
        return EMPTY;
    }

    public static FileExtension extractExt(String filePath) {
        int startPos = filePath.lastIndexOf('.');
        if (startPos < 0) {
            return EMPTY;
        } else {
            return parse(filePath.substring(startPos));
        }
    }

    public static String removeExt(String filePath) {
        int endPos = filePath.lastIndexOf('.');
        if (endPos < 0) {
            return filePath;
        } else {
            return filePath.substring(0, endPos);
        }
    }

    public static String appendExt(String filePathNoExt, FileExtension ext) {
        return filePathNoExt + ext.getId();
    }

    @Override
    public String toString() {
        return getId();
    }
}
